package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarCrossProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityRankMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Cmd(code = GameProtocol.S_STRONGHOLD_TRADE_DETAIL, desc = "通商详情")
public class SrtongholdTradeDetailCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdTradeDetailRespMsg.Builder resp = StrongholdWarProto.StrongholdTradeDetailRespMsg.newBuilder();
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
        }
        int activityId = activityInfo.getActivityId();
      /*  UnionActivityConditionData unionActivityConditionData = StrongholdActivityRankMgr.getUnionActivityConditionData(activityId, StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION, player.getUserId());
        if (unionActivityConditionData != null) {
            List<Long> userIdList = unionActivityConditionData.getMemberUserIdList();
            Set<Long> userSet = StrongholdWarMgr.getUnTradeUserList(activityId, userIdList);
            long serverId = GameServer.getInstance().getServerId();
            for (Long userId : userSet) {
                CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(userId);
                if (unionMember == null) {
                    continue;
                }
                StrongholdWarProto.StrongholdTradeMemberDataMsg.Builder msg = StrongholdWarProto.StrongholdTradeMemberDataMsg.newBuilder();
                if(unionMember.getServerId() == GameServer.getInstance().getServerId()){
                    msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(userId, serverId)));
                }else {
                    msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo()));
                }
                msg.setUserId(userId);
                msg.setTodayContribution(unionMember.getTodayContribution());
                msg.setTotalContribution(unionMember.getTotalContribution());
                resp.addMemberData(msg);
            }
        }
        player.sendChannelPacket(ClientProtocol.U_STRONGHOLD_TRADE_DETAIL, resp);*/
        StrongholdWarCrossProto.CrossStrongholdTradeDetailReqMsg.Builder req = StrongholdWarCrossProto.CrossStrongholdTradeDetailReqMsg.newBuilder();
        req.setActivityId(activityId);
        Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(player.getUserInfo().getUnionUid());
        if (unionMemberMap != null) {
            req.addAllMemberId(unionMemberMap.keySet());
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_TRADE_DETAIL, req, req.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
